﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Caching;
namespace DataLibrary
{
    public class SQLData
    {
        private String _strMyconnecString;
        private SqlCommand _sqlCommand;
        private SqlConnection _sqlConnec;
        private String _strErr;
        private Cache _cache = HttpContext.Current.Cache;
        private const int _time = 30;
        private String _strMaHoa = String.Empty;
        public SQLData() { }
        public SQLData(String myconnecString) {
             _strMyconnecString=myconnecString;
        }
        public String MyConnecString
        {
            get
            {
                return this._strMyconnecString;
            }
            set
            {
                this._strMyconnecString = value;
            }
        }

        public SqlConnection Connection {
            get { return new SqlConnection(_strMyconnecString); }
        }

        public String Err
        {
            get
            {
                return this._strErr;
            }
            set
            {
                this._strErr = value;
            }
        }

        public void Open()
        {
            try
            {

                _sqlConnec = new SqlConnection(_strMyconnecString);
                _sqlConnec.Open();
            }
            catch (Exception err)
            {
                this._strErr = err.Message.ToString();
            }
        }

        public void Close()
        {
            if (_sqlConnec.State == ConnectionState.Open)
            {
                _sqlConnec.Close();
            }
        }

        public Boolean ExecuteNonQuery(String parameter, Object value, String commandText, CommandType commandType)
        {
            Boolean bBool = false;
            try
            {
                Open();
                _sqlCommand = new SqlCommand();
                _sqlCommand.CommandText = commandText;
                _sqlCommand.Connection = _sqlConnec;
                _sqlCommand.CommandType = commandType;
                _sqlCommand.Parameters.AddWithValue("@" + parameter.Trim(), value);
                _sqlCommand.ExecuteNonQuery();
                bBool = true;

            }
            catch (Exception err)
            {
                this._strErr = err.Message.ToString();
                bBool = false;
            }
            Close();
            return bBool;
        }

        public int ExecuteNonQuery(String strSQL, CommandType commandType, params SqlParameter[] arrParameter)
        {
            int id = -1;
            try
            {
                Open();
                _sqlCommand = new SqlCommand();
                _sqlCommand.CommandText = strSQL;
                _sqlCommand.Connection = _sqlConnec;
                _sqlCommand.CommandType = commandType;
                _sqlCommand.Parameters.AddRange(arrParameter);
                id=_sqlCommand.ExecuteNonQuery();
                
            }
            catch
            {
            }
            Close();
            return id;
        }


   

    }
}
